#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    string longestPalindrome(string s) {
        int n = s.size(), len = 1, head = 0;
        vector<vector<bool>> dp(n, vector<bool>(n));
        for (int i = 0; i < n; i++)
            for (int j = 0; j <= i; j++)
                if ((s[i] == s[j]) && (dp[i][j] = i <= j + 1 ? true : dp[i - 1][j + 1]) && i - j + 1 > len)
                    head = j, len = i - j + 1;
        return s.substr(head, len);
    }
};